package com.velociti.ikarus.ui.widget;

import java.text.DecimalFormatSymbols;
import java.util.Locale;

import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.ui.ClientWidget;
import com.velociti.ikarus.widget.client.ui.VIkarusNumericField;

@ClientWidget(VIkarusNumericField.class)
public class IkarusNumericField extends IkarusTextField {
	public static String RIGHT = "right";
	public static String LEFT = "left";

	private String alignment = RIGHT;
	private String decimalSeperator = ".";

	private String groupingSeperator = ",";
	private int scaleSize = 2;
	private int precisionSize = 40;
	private boolean useGroupingSeperator = true;

	public IkarusNumericField() {
		super.setTextType(IkarusTextField.TEXTTYPE_DIGIT);

	}

	public IkarusNumericField(String caption) {
		super(caption);
		super.setTextType(IkarusTextField.TEXTTYPE_DIGIT);

	}

	public void setLocale(Locale locale) {
		DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
		decimalSeperator = String.valueOf(symbols.getDecimalSeparator());
		groupingSeperator = String.valueOf(symbols.getGroupingSeparator());
		requestRepaint();
	}

	public void setTextType(String textType) {
		throw new UnsupportedOperationException();
	}

	public void setExceptionalChars(String exceptionalChars) {
		throw new UnsupportedOperationException();
	}

	public String getDecimalSeperator() {
		return decimalSeperator;
	}

	public void setDecimalSeperator(String decimalSeperator) {
		this.decimalSeperator = decimalSeperator;
		requestRepaint();
	}

	public String getGroupingSeperator() {
		return groupingSeperator;
	}

	public void setGroupingSeperator(String groupingSeperator) {
		this.groupingSeperator = groupingSeperator;
		setUseGroupingSeperator(groupingSeperator != null);
		requestRepaint();
	}

	public int getScaleSize() {
		return scaleSize;
	}

	public void setScaleSize(int scaleSize) {
		this.scaleSize = scaleSize;
		requestRepaint();
	}

	public int getPrecisionSize() {
		return precisionSize;
	}

	public void setPrecisionSize(int precisionSize) {
		this.precisionSize = precisionSize;
		requestRepaint();
	}

	@Override
	public void paintContent(PaintTarget target) throws PaintException {
		super.paintContent(target);
		target.addAttribute("decimalsep", decimalSeperator);
		target.addAttribute("groupingsep", groupingSeperator);
		target.addAttribute("scale", scaleSize);
		target.addAttribute("precision", precisionSize);
		target.addAttribute("usegrouping", useGroupingSeperator);
		target.addAttribute("alignment", alignment);

	}

	public boolean isUseGroupingSeperator() {
		return useGroupingSeperator;
	}

	public void setUseGroupingSeperator(boolean useGroupingSeperator) {
		this.useGroupingSeperator = useGroupingSeperator;
	}

	public String getTextAlignment() {
		return alignment;
	}

	public void setTextAlignment(String alignment) {
		this.alignment = alignment;
	}

}
